package com.lm.survey.util;



import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import com.lm.survey.model.User;

public class RequestToPojo {
	static {
	    DateConverter d = new DateConverter();
	   
	    ConvertUtils.register(d, java.util.Date.class);
	  }

	  public static void populate(HttpServletRequest request, Object obj) {
	    Map map = request.getParameterMap();
	    try {
	      BeanUtils.populate(obj, map);
	    } catch (IllegalAccessException e) {
	      throw new RuntimeException(e.getMessage());
	    } catch (InvocationTargetException e) {
	    	throw new RuntimeException(e.getMessage());
	    }
	  }
	  
	  public static void  main(String[] args) throws Exception{
		  User user = new User();
		  Map map = new HashMap();
		  map.put("userName", "9999");
		  map.put("password", "2001-1-1");
		  BeanUtils.populate(user, map);
		  System.out.println(user.getPassword());
		  
	  }
	  
	 
}
class DateConverter implements Converter{
	  public Object convert(Class type, Object value){
		  SimpleDateFormat sd = new SimpleDateFormat("yyyy-mm-dd");
		  if(value==null)return null;
		  try {
          return sd.parse((String)value);
      } catch (ParseException e) {
          // TODO Auto-generated catch block
          throw new RuntimeException(e.getMessage());
      }
	  }
}

